<?php
//require_once(SERVER_ROOT.'conf\\readconf.php');
include_once "shared/ez_sql_core.php";
include_once "mysqli/ez_sql_mysqli.php";
class xdb{
    private  $_db;
    private static $_instance;
    
	private function __construct(){
	    $dbconf=new readconf("dbcon.conf");
	    $confarray=$dbconf->getarray();
	    $this->_db = new ezSQL_mysqli($confarray["db_user"],$confarray["db_pwd"],$confarray["db_name"],$confarray["db_host"],$confarray["db_charset"]);
	    /*
		mysqli_query($this->_db,"set character set '".$confarray["db_charset"]."'");//读库 
		mysqli_query($this->_db,"set names '".$confarray["db_charset"]."'");//写库
		*/
	}
	
	public static function getInstance(){
	    if(! (self::$_instance instanceof self) ) {
	        self::$_instance = new self();
	    }
	    return self::$_instance;
	}
	
	public function getdb(){
	    return self::getInstance()->_db;
	}
	
	public function getvar($q){
	    return self::getInstance()->getdb()->get_var($q);
	}
	public function getrow($q){
	    return self::getInstance()->getdb()->get_row($q,ARRAY_A);
	}
    public  function getjson($query,$arr="ARRAY_A"){
	    $result=array();
        $res=self::getInstance()->getdb()->get_results($query,$arr);
        foreach ( $res as $key=>$re ){
            $result[$key]=$re;
        }
	    return json_encode($result);
	}
	public  function getarray($query,$arr="ARRAY_A"){
	    $result=array();
	    $res=self::getInstance()->getdb()->get_results($query,$arr);
	    foreach ( $res as $key=>$re ){
	        $result[$key]=$re;
	    }
	    return $result;
	}
	
	public function query($query){
		self::getInstance()->getdb()->query($query);
	}
	
	public function indb($dbtable,$array,$array2){
	    $query="insert into $dbtable (";
	    $query1=$query2="";
	    $query3="select id from $dbtable where ";
	    foreach ($array as $key => $value){
	        $query1.=$key.",";
	        $query2.="'".$value."',";
	    }
	    foreach ($array2 as $key2 => $value2){
	        $query3.=$key2."='".$value2."' and ";
	    }
	    $query.=rtrim($query1,",").")values(".rtrim($query2,",").")";
	    $query3=rtrim($query3," and ");
	    $query=htmlspecialchars($query);
	    $query=urldecode($query);
	    $query3=htmlspecialchars($query3);
	    $query3=urldecode($query3);
	    //echo $query;exit();
	    if(self::getInstance()->getdb()->get_var($query3)){
	        //echo "xxf";
	        return 0;
	    }else{
	        //echo $query;
    	    if (self::getInstance()->getdb()->query($query)){
    	        return 1;//无语法错误返回1
    	    }else{
    	        //echo "zz";
    	        return 0;
    	    }
	    }
	}
	
	public function getcol($q){
	    return self::getInstance()->getdb()->get_col($q);
	}
	
	public function updb($table,$array,$array2){
	    $q="update $table set ";
	    $q1=$q2="";
	    foreach ($array as $k=>$v){
	        $q1.=$k."='".$v."',";
	    }
	    foreach ($array2 as $k2=>$v2){
	        $q2.=$k2." like '".$v2."' and ";
	    }
	    $q.=rtrim($q1,",")." where ".rtrim($q2," and ");
	    //echo $q;
	    return self::getInstance()->getdb()->query($q);
	}
	
	public function deldb($table,$array){
	    $q="delete from $table where ";
	    $q1="";
	    foreach ($array as $k=>$v){
	        $q1.=$k." like '".$v."' and ";
	    }
	    $q.=rtrim($q1," and ");
	    return self::getInstance()->getdb()->query($q);
	}

}
?>